package 课程设计报告_hb_7_1;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.junit.Test;

/**
 * @author Jiang Zhiyan
 *
 * 2021年7月1日 上午10:19:08
 *
 */
public class ColletionsTest {
     public void readTest() {
    	 List<String> list = new ArrayList<>();
    	 Collections.addAll(list, "abc","124","456");
    	 //将list设置为只读
    	 List<String> newList =Collections.unmodifiableList(list);
    	 System.out.println(newList);
    	 //newList.add("qqq");
    	 //newList.set(0,"qqq");
    	 Collections.shuffle(list);
    	 System.out.println(list);
     }
     
     //定义一个排序规则，根据字符串的长度进行排序
     class SortByStringLength implements Comparator<String>{
    	 @Override	
    	 public int compare(String o1,String o2) {
    		 return o1.length()-o2.length();
    	 }
    	 
     }
     
     @Test
     public void sortTest() {
    	 List<String> list = new ArrayList<>();
    	 Collections.addAll(list, "array","cba","carry","x","body","do");
    	 Collections.sort(list);
    	 System.out.println(list);
    	 Collections.sort(list,new SortByStringLength());
    	 System.out.println(list);
    	 List<Apple> apps = new ArrayList<Apple>();
    	 Collections.addAll(apps, new Apple("绿色",150,"山西"),
    			 new Apple("绿色",150,"山西"),
    			 new Apple("绿色",100,"山西"),
    			 new Apple("绿色",90,"山西"));
    	 apps.add(new Apple ("绿色",100,"陕西"));
    	 apps.add(new Apple ("绿色",90,"陕西"));

    	 apps.add(new Apple ("红色",90,"山西"));
    	 apps.add(new Apple ("绿色",150,"陕西"));
    	 Collections.sort(apps);
    	 for(Apple apple : apps) {
    		 System.out.println(apple);
    	 }
     }

}
